<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Example:
 * 
 * $this->load->library('image');
 * $this->image->loadImage('a.jpg');
 * $this->image->resize(1000,500);
 * $this->image->save('b.jpg');
 * $this->image->destroy();
 */

class Image {

	const IMAGE_LIBRARY_GD = 'gd';
	const IMAGE_LIBRARY_IMAGEMAGICK = 'imagemagick';

	protected $_library = Image::IMAGE_LIBRARY_GD;

	protected $_image = null;
	protected $_type = null;

	public function __construct(){
		if(class_exists('Imagick')){
			$this->_library = Image::IMAGE_LIBRARY_IMAGEMAGICK;
		}
	}

	public function setLibrary($lib){
		$this->_library = $lib;
	}

	public function loadImage($path){
		$size = getimagesize($path); 

		$this->_type = $size['mime'];

		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			$this->_image = new Imagick($path);
			$this->_image->stripImage();
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			if($this->_type == 'image/jpeg'){
				$this->_image = imagecreatefromjpeg($path);
			}elseif($this->_type == 'image/png'){
				$this->_image = imagecreatefrompng($path);
			}
		}
	}

	public function resize($width,$height){
		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			if($width != 0 && $height != 0){
				$orig = $this->_image->getImageGeometry();
				$fxWidth = $orig['width'] / $width;
				$fxHeight = $orig['height'] / $height;

				if($fxWidth > $fxHeight){
					$this->_image->scaleImage(0,$height);
					$orig = $this->_image->getImageGeometry();
					$targetX = intval(($orig['width'] - $width) / 2);
					$this->_image->cropImage($width,$height,$targetX,0);
				}elseif($fxWidth < $fxHeight){
					$this->_image->scaleImage($width,0);
					$orig = $this->_image->getImageGeometry();
					$targetY = intval(($orig['height'] - $height) / 2);
					$this->_image->cropImage($width,$height,0,$targetY);
				}else{
					$this->_image->scaleImage($width,0);
				}
			}elseif($width != 0){
				$this->_image->scaleImage($width,0);
			}elseif($height != 0){
				$this->_image->scaleImage(0,$height);
			}
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			$origWidth = imagesx($this->_image);
			$origHeight = imagesy($this->_image);

			if($width != 0 && $height != 0){
				$dest = imagecreatetruecolor($width,$height);
				$fxWidth = $origWidth / $width;
				$fxHeight = $origHeight / $height;

				if($fxWidth > $fxHeight){
					$srcWidth = intval($origHeight / $height * $width);
					$targetX = intval(($origWidth - $srcWidth)/2);

					imagecopyresized($dest,$this->_image,0,0,$targetX,0,$width,$height,$srcWidth,$origHeight);
				}elseif($fxWidth < $fxHeight){
					$srcHeight = intval($origWidth / $width * $height);
					$targetY = intval(($origHeight - $srcHeight)/2);

					imagecopyresized($dest,$this->_image,0,0,0,$targetY,$width,$height,$origWidth,$srcHeight);
				}else{
					imagecopyresized($dest,$this->_image,0,0,0,0,$width,$height,$origWidth,$origHeight);
				}
			}elseif($width != 0){
				$height = $width / $origWidth * $origHeight;
				$dest = imagecreatetruecolor($width,$height);
				imagecopyresized($dest,$this->_image,0,0,0,0,$width,$height,$origWidth,$origHeight);
			}elseif($height != 0){
				$width = $height / $origHeight * $origWidth;
				$dest = imagecreatetruecolor($width,$height);
				imagecopyresized($dest,$this->_image,0,0,0,0,$width,$height,$origWidth,$origHeight);
			}
			imageDestroy($this->_image);
			$this->_image = $dest;
		}
	}

	public function addCanvas($width,$height,$color = 'white'){
		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			$canvas = new Imagick();
			$canvas->newImage($width,$height,$color,$this->_type);

			$orig = $this->_image->getImageGeometry();
			$fxWidth = ($width - $orig['width']) / 2;
			$fxHeight = ($height - $orig['height']) / 2;

			$canvas->compositeImage($this->_image,imagick::COMPOSITE_OVER,$fxWidth,$fxHeight);
			$this->_image->clear();
			$this->_image->destroy();
			$this->_image = $canvas;
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			$origWidth = imagesx($this->_image);
			$origHeight = imagesy($this->_image);
			$fxWidth = ($width - $origWidth) / 2;
			$fxHeight = ($height - $origHeight) / 2;
			$canvas = imagecreatetruecolor($width,$height);
			if($color == 'white'){
				$bgcolor = imagecolorallocate($canvas,255,255,255);
			}else{
				$bgcolor = imagecolorallocate($canvas,0,0,0);
			}
			imagefill($canvas,0,0,$bgcolor);
			imagecopyresized($canvas,$this->_image,$fxWidth,$fxHeight,0,0,$origWidth,$origHeight,$origWidth-$fxWidth,$origHeight-$fxHeight);
			imageDestroy($this->_image);
			$this->_image = $canvas;
		}
	}

	public function waterMask(){
		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			$mask = new Imagick(PATH_WATER_MASK_FILE);
			$origSize = $this->_image->getImageGeometry();
			$maskSize = $mask->getImageGeometry();
			$this->_image->compositeImage($mask,imagick::COMPOSITE_DEFAULT,intval(($origSize['width'] - $maskSize['width']) / 2),intval(($origSize['height'] - $maskSize['height']) / 2));
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			$origWidth = imagesx($this->_image);
			$origHeight = imagesy($this->_image);
			$mask = imagecreatefrompng(PATH_WATER_MASK_FILE);
			$maskWidth = imagesx($mask);
			$maskHeight = imagesy($mask);
			imagecopymerge($this->_image,$mask,intval(($origWidth - $maskWidth) / 2),intval(($origHeight - $maskHeight) / 2),0,0,$maskWidth,$maskHeight,40);
		}
	}

	public function save($path){
		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			$this->_image->writeImage($path);
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			if($this->_type == 'image/jpeg'){
				imagejpeg($this->_image,$path,100);
			}elseif($this->_type == 'image/png'){
				imagepng($this->_image,$path,9);
			}
		}
	}

	public function display(){
		header("Content-Type: image/".$this->_type);		
		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			echo $this->_image;
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			if($this->_type == 'image/jpeg'){
				imagejpeg($this->_image,null,100);
			}elseif($this->_type == 'image/png'){
				imagepng($this->_image,null,9);
			}
		}
	}

	public function destroy(){
		if($this->_library == Image::IMAGE_LIBRARY_IMAGEMAGICK){
			$this->_image->clear();
			$this->_image->destroy();
		}elseif($this->_library == Image::IMAGE_LIBRARY_GD){
			imageDestroy($this->_image);
		}

		$this->_image = null;
	}
}

/* End of file image.php */
/* Location: ./application/libraries/image.php */